using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace 修为尽失.辅助
{
    public enum 特别标记
    {
        无标记,
        标记一,
        标记二,
        标记三,
        标记四,
        标记五
    }


    public static class AttachedProperty
    {
        #region 特别标记属性

        public static readonly DependencyProperty 特别标记属性 = DependencyProperty.RegisterAttached("特别标记", typeof(特别标记),
            typeof(AttachedProperty), new FrameworkPropertyMetadata(特别标记.无标记));

        public static 特别标记 Get特别标记(DependencyObject d)
        {
            return (特别标记) d.GetValue(特别标记属性);
        }

        public static void Set特别标记(DependencyObject d, 特别标记 v)
        {
            d.SetValue(特别标记属性, v);
        }

        #endregion

        public static readonly DependencyProperty LastScrollValueProperty =
            DependencyProperty.RegisterAttached("LastScrollValue", typeof(double), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(default(double)));

        public static double GetLastScrollValue(DependencyObject d)
        {
            return (double) d.GetValue(LastScrollValueProperty);
        }

        public static void SetLastScrollValue(DependencyObject o, double d)
        {
            o.SetValue(LastScrollValueProperty, d);
        }

        #region EncodingIconProperty 字体图标

        /// <summary>
        /// 字体图标
        /// </summary>
        public static readonly DependencyProperty EncodingIconProperty =
            DependencyProperty.RegisterAttached("EncodingIcon", typeof(string), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(""));

        public static string GetEncodingIcon(DependencyObject d)
        {
            return (string) d.GetValue(EncodingIconProperty);
        }

        public static void SetEncodingIcon(DependencyObject obj, string value)
        {
            obj.SetValue(EncodingIconProperty, value);
        }

        #endregion

        #region EncodingIconSizeProperty 字体图标大小

        /// <summary>
        /// 字体图标
        /// </summary>
        public static readonly DependencyProperty EncodingIconSizeProperty =
            DependencyProperty.RegisterAttached("EncodingIconSize", typeof(double), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(18D));

        public static double GetEncodingIconSize(DependencyObject d)
        {
            return (double) d.GetValue(EncodingIconSizeProperty);
        }

        public static void SetEncodingIconSize(DependencyObject obj, double value)
        {
            obj.SetValue(EncodingIconSizeProperty, value);
        }

        #endregion

        #region EncodingIconMarginProperty 字体图标边距

        /// <summary>
        /// 字体图标
        /// </summary>
        public static readonly DependencyProperty EncodingIconMarginProperty =
            DependencyProperty.RegisterAttached("EncodingIconMargin", typeof(Thickness), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(null));

        public static Thickness GetEncodingIconMargin(DependencyObject d)
        {
            return (Thickness) d.GetValue(EncodingIconMarginProperty);
        }

        public static void SetEncodingIconMargin(DependencyObject obj, Thickness value)
        {
            obj.SetValue(EncodingIconMarginProperty, value);
        }

        #endregion

        #region AllowsAnimationProperty 启用旋转动画

        /// <summary>
        /// 启用旋转动画
        /// </summary>
        public static readonly DependencyProperty AllowsAnimationProperty =
            DependencyProperty.RegisterAttached("AllowsAnimation", typeof(bool), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(false, AllowsAnimationChanged));

        public static bool GetAllowsAnimation(DependencyObject d)
        {
            return (bool) d.GetValue(AllowsAnimationProperty);
        }

        public static void SetAllowsAnimation(DependencyObject obj, bool value)
        {
            obj.SetValue(AllowsAnimationProperty, value);
        }

        /// <summary>
        /// 旋转动画
        /// </summary>
        private static DoubleAnimation _rotateAnimation =
            new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(200)));

        /// <summary>
        /// 绑定动画事件
        /// </summary>
        private static void AllowsAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uc = d as FrameworkElement;
            if (uc == null)
                return;
            if (uc.RenderTransformOrigin == new Point(0, 0))
            {
                uc.RenderTransformOrigin = new Point(0.5, 0.5);
                RotateTransform trans = new RotateTransform(0);
                uc.RenderTransform = trans;
            }

            var value = (bool) e.NewValue;
            if (value)
            {
                _rotateAnimation.To = 360;
                // var ani = ((DoubleAnimation) Application.Current.TryFindResource("菜单旋转动画"));
                uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, _rotateAnimation);
            }
        }

        #endregion

        #region CornerRadiusProperty Border圆角

        /// <summary>
        /// Border圆角
        /// </summary>
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(AttachedProperty),
                new FrameworkPropertyMetadata(null));

        public static CornerRadius GetCornerRadius(DependencyObject d)
        {
            return (CornerRadius) d.GetValue(CornerRadiusProperty);
        }

        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }

        #endregion
    }
}